home *** CD-ROM | disk | FTP | other *** search
/ Internet Tools (InfoMagic) / Internet Tools.iso / news / moderating / multimod.shar.Z / multimod.shar / multimod.c next >
C/C++ Source or Header  |  1994-07-21  |  3KB  |  119 lines

  1. /* This program is designed to implement multiple moderators. It is designed
  2.  * to be called by sendmail when mail is sent to a moderated group alias, with
  3.  * the name of a file containing up to 20 moderator addresses as argument, and
  4.  * randomly send the message to one of the moderator addresses
  5.  *
  6.  *
  7.  * Usage:
  8.  *
  9.  *    multimod [-a] [-c cc-address] file-of-moderators
  10.  *
  11.  *    The message should be on the standard input. This is designed to be
  12.  *    mailed to via an alias, e.g. for sendmail's /usr/lib/aliases:
  13.  *
  14.  * feminism:  "|/usr/local/bin/multimod /usr/local/lib/feminism.moderators"
  15.  *
  16.  * Arguments:
  17.  *
  18.  *    Other than flags, only one: the name of a file containing moderator
  19.  *    addresses, one per line.
  20.  * 
  21.  * Flags:
  22.  *
  23.  *     -a: send to all the moderators in the file. This is useful if you want
  24.  *         to have an alias that will reach all the moderators
  25.  *
  26.  *     -c: indicates that the next argument is an address to which a copy
  27.  *         of the article should be sent. This is useful if you want to
  28.  *         archive all the articles submitted.
  29.  */
  30. #include <stdio.h>
  31. FILE *mod; /* moderators file */
  32. #define SHIFT (++argv,--argc)
  33. #define MAXMOD 20
  34. #define POSTMASTER "postmaster@ncar.ucar.edu"
  35. main(argc,argv) char **argv; int argc; {
  36. char *addr[MAXMOD];  /* array of moderator addresses */
  37. char line[1024]; /* buffer for reading in moderator addresses */
  38. char *cc=NULL; /* cc: address */
  39. register int i;
  40. int all=0; /* send to all moderators */
  41. void error();
  42. char *malloc();
  43. void send_mail_to();
  44.  
  45. while(SHIFT && **argv == '-') {  /* process flags */
  46.    if (strlen(*argv) != 2) {
  47.       sprintf(line,"Unknown option: %s\n",*argv);
  48.       error(line);
  49.    }
  50.    switch(*(*argv+1)) {
  51.       case 'a':  /* send to all moderators */
  52.      if (cc != NULL) error("May not use both -a and -c\n");
  53.      all++;
  54.      break;
  55.       case 'c': /* send copy to designated next argument address */
  56.      if (! SHIFT) error("-c option requires value\n");
  57.      if (all) error("May not use both -a and -c\n");
  58.      cc= *argv;
  59.      break;
  60.       default:
  61.          sprintf(line,"Unknown flag: %s\n",*argv);
  62.          error(line);
  63.    }
  64. }
  65.  
  66. if (argc != 1) error("Usage: multimod [-a] [-c copyto] moderator-file\n");
  67.  
  68. if ((mod=fopen(*argv,"r")) == NULL) {
  69.    sprintf(line,"Cannot open %s\n",*argv);
  70.    error(line);
  71. }
  72.  
  73. for (i=0; i < MAXMOD; i++) {
  74.    if (fgets(line,1024,mod) == NULL) break;
  75.    if (line[0] == '#') { --i; continue; }
  76.    if (line[strlen(line)-1] == '\n') line[strlen(line)-1]=0; /* strip newline */
  77.    if ((addr[i]=malloc(strlen(line))) == NULL) error("Out of memory\n");
  78.    strcpy(addr[i],line);
  79. }
  80. if (i >= MAXMOD) error("Too many moderators\n");
  81.  
  82. if (all) {
  83.    register int j;
  84.    for(line[0]=0,j=0; j < i; j++) {
  85.       strcat(line,addr[j]);
  86.       strcat(line," ");
  87.    }
  88.    printf("%s\n",line);
  89.    send_mail_to(line);
  90. } else {
  91.    i=time(NULL)%i;
  92.    printf("%s\n",addr[i]);
  93.    strcpy(line,addr[i]);
  94.    if (cc != NULL) {
  95.       strcat(line," ");
  96.       strcat(line,cc);
  97.    }
  98.    send_mail_to(line);
  99. }
  100. exit(0);
  101. }
  102.  
  103. void
  104. error(msg) char *msg; {   /* print error message and exit */
  105. void send_mail_to();
  106. fputs(msg,stdout);
  107. send_mail_to(POSTMASTER);
  108. exit(1);
  109. }
  110.  
  111. void
  112. send_mail_to(s) char *s; { /*exec sendmail and deliver to designated address */
  113.  
  114. execl("/usr/lib/sendmail","sendmail",s,0);
  115. perror("multimod: /usr/lib/sendmail");
  116. exit(1);
  117. }
  118.  
  119.